<template>
  <div class="card content-box">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="140px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item :label="t('config.ad.title')" prop="title">
            <el-input v-model="ruleForm.title" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="t('config.ad.type')" prop="type">
            <el-input v-model="ruleForm.type" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="t('config.ad.href')" prop="href">
            <el-input v-model="ruleForm.href" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label="t('config.ad.image')" prop="image">
            <Resources v-model="ruleForm.image" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">保存</el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
</template>

<script setup lang="ts" name="dynamicForm">
import { reactive, ref, onBeforeMount } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { adCreate, adUpdate, adDetail } from "@/api/modules/config";
import { useTabsStore } from "@/stores/modules/tabs";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import Resources from "@/components/Resources/index.vue";
const route = useRoute();
const { t } = useI18n();

const ruleForm = reactive({
  id: route.params.id,
  title: "",
  href: "",
  image: "",
  type: ""
});

const tabStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();

const ruleFormRef = ref<FormInstance>();

const rules = reactive<FormRules>({
  title: [{ required: true, message: t("config.ad.rule.title"), trigger: "blur" }],
  resources_id: [{ required: true, message: t("config.ad.rule.resources"), trigger: "blur" }],
  type: [{ required: true, message: t("config.ad.rule.type"), trigger: "blur" }]
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(valid => {
    if (valid) {
      const action = ruleForm.id > 0 ? adUpdate : adCreate;
      action(ruleForm).then(response => {
        ElMessage.success(response.message);
        setTimeout(() => {
          if (route.meta.isAffix) return;
          tabStore.removeTabs(route.fullPath);
          keepAliveStore.removeKeepAliveName(route.name as string);
        }, 200);
      });
    } else {
      ElMessage.error("请根据提示输入信息！");
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const toDetail = id => {
  adDetail({ id }).then(response => {
    ruleForm.id = response.data.id;
    ruleForm.image = response.data.image;
    ruleForm.title = response.data.title;
    ruleForm.type = response.data.type;
    ruleForm.href = response.data.href;
  });
};

onBeforeMount(() => {
  if (route.params.id) toDetail(route.params.id);
});
</script>

<style scoped lang="scss">
@import "./edit.scss";
</style>
